Godot Engine Game Development in 24 Hours, Sams Teach Yourself: The Official Guide to Godot 3.0 by Ariel Manzur & George Marques

Godot Engine Game Development in 24 Hours, Sams Teach Yourself: The Official Guide to Godot 3.0 by Ariel Manzur & George Marques

Author:Ariel Manzur & George Marques
Language: eng
Format: epub, pdf
Publisher: Sams Publishing
Published: 2018-03-22T04:00:00+00:00


Loading Configuration Files

After you save the file to disk, only half the work is done. When the game starts again, you need to load the file from the disk and change the options to match the file. ConfigFile will also help you with that (see Listing 12.2).

LISTING 12.2 Loading Configurations

Click here to view code image

extends Node func _ready(): print(load_config()) # Should do something meaningful, but let's print for test purposes func load_config(): var path = "user://config.ini" # The path to load the file var config = ConfigFile.new() # Create a new ConfigFile object var default_options = { # Create a dictionary of default options "difficulty": "easy", "music_volume": 80 } var err = config.load(path) # Load the file from the disk if err != OK: # If there's an error return the default options return default_options var options = {} # Create a dictionary to store the loaded options # Get the values from the file or the predefined defaults if missing options.difficulty = config.get_value("options", "difficulty", default_options. difficulty) options.music_volume = config.get_value("audio", "music_volume", default _options.music_volume) return options # Return the loaded options



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.